import java.util.List;
import java.util.Scanner;

public class UserService {
    private Scanner scanner;
    private UserDAO userDAO;

    public UserService(Scanner scanner) {
        this.scanner = scanner;
        this.userDAO = new UserDAO();
    }

    // 用户登录
    public User login() {
        System.out.print("请输入用户名: ");
        String username = scanner.nextLine();
        System.out.print("请输入密码: ");
        String password = scanner.nextLine();

        User user = userDAO.findUserByUsername(username);
        if (user != null && user.getPassword().equals(password)) {
            System.out.println("登录成功！欢迎您，" + user.getName() + "(" + user.getRole() + ")");
            return user;
        } else {
            System.out.println("用户名或密码错误！");
            return null;
        }
    }

    // 列出所有用户
    public void listAllUsers() {
        List<User> users = userDAO.getAllUsers();
        System.out.println("\n=== 用户列表 ===");
        System.out.println("用户名\t角色\t姓名\t电话");
        System.out.println("--------------------------------");
        for (User user : users) {
            System.out.println(String.format("%s\t%s\t%s\t%s",
                    user.getUsername(), user.getRole(), user.getName(), user.getPhone()));
        }
    }

    // 添加用户
    public boolean addUser() {
        System.out.print("请输入用户名: ");
        String username = scanner.nextLine();

        // 检查用户是否已存在
        if (userDAO.findUserByUsername(username) != null) {
            System.out.println("用户名已存在！");
            return false;
        }

        System.out.print("请输入密码: ");
        String password = scanner.nextLine();
        System.out.print("请输入角色(admin/manager/user): ");
        String role = scanner.nextLine();
        System.out.print("请输入姓名: ");
        String name = scanner.nextLine();
        System.out.print("请输入电话: ");
        String phone = scanner.nextLine();

        User user = new User(username, password, role, name, phone);
        boolean result = userDAO.addUser(user);
        if (result) {
            System.out.println("用户添加成功！");
        } else {
            System.out.println("用户添加失败！");
        }
        return result;
    }

    // 删除用户
    public boolean deleteUser(String username) {
        // 通过公共方法获取当前用户，避免直接访问私有成员
        if (username.equals(CinemaSystem.getCurrentUser().getUsername())) {
            System.out.println("不能删除当前登录用户！");
            return false;
        }

        User user = userDAO.findUserByUsername(username);
        if (user == null) {
            System.out.println("用户不存在！");
            return false;
        }

        boolean result = userDAO.deleteUser(username);
        if (result) {
            System.out.println("用户删除成功！");
        } else {
            System.out.println("用户删除失败！");
        }
        return result;
    }

    // 重置用户密码
    public boolean resetPassword(String username, String newPassword) {
        User user = userDAO.findUserByUsername(username);
        if (user == null) {
            System.out.println("用户不存在！");
            return false;
        }

        boolean result = userDAO.updatePassword(username, newPassword);
        if (result) {
            System.out.println("密码重置成功！");
        } else {
            System.out.println("密码重置失败！");
        }
        return result;
    }

    // 修改当前用户密码
    public boolean changePassword(User user, String oldPassword, String newPassword) {
        // 验证原密码
        if (!user.getPassword().equals(oldPassword)) {
            System.out.println("原密码错误，修改失败！");
            return false;
        }

        boolean result = userDAO.updatePassword(user.getUsername(), newPassword);
        if (result) {
            System.out.println("密码修改成功，请重新登录！");
            // 调用CinemaSystem的public静态方法进行重新登录
            CinemaSystem.loginProcess();
        } else {
            System.out.println("密码修改失败！");
        }
        return result;
    }
}
